﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace EntityFrameworkCore.Model
{

    public class EntityBase:EntityBase<Guid,Guid?>
    {

      

    }
    /// <summary>
    /// 这里应该把创建用户与修改类型也做做泛型
    /// </summary>
    /// <typeparam name="TPrimaryKey"></typeparam>
    public class EntityBase<TPrimaryKey,TUserKey>
    {

        /// <summary>
        /// 主键
        /// </summary>
        public virtual TPrimaryKey Id { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public  virtual DateTime CreateTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public virtual TUserKey CreateUserId { get; set; }

        /// <summary>
        /// 最后修改人
        /// </summary>
        public virtual TUserKey ModifiedUserId {get;set;}

        /// <summary>
        /// 是否删除
        /// </summary>
        public virtual bool IsDelete { get; set; }


        /// <summary>
        /// 最后修改时间
        /// </summary>

        public virtual DateTime ModifiedTime { get; set; }

        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is EntityBase<TPrimaryKey, TUserKey>))
            {
                return false;
            }

            //Same instances must be considered as equal
            if (ReferenceEquals(this, obj))
            {
                return true;
            }

            var other = (EntityBase<TPrimaryKey, TUserKey>)obj;
            //Must have a IS-A relation of types or must be same type
            var typeOfThis = GetType();
            var typeOfOther = other.GetType();
            if (!typeOfThis.GetTypeInfo().IsAssignableFrom(typeOfOther) && !typeOfOther.GetTypeInfo().IsAssignableFrom(typeOfThis))
            {
                return false;
            }

            return Id.Equals(other.Id);
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        public static bool operator ==(EntityBase<TPrimaryKey, TUserKey> left, EntityBase<TPrimaryKey, TUserKey> right)
        {
            if (Equals(left, null))
            {
                return Equals(right, null);
            }

            return left.Equals(right);
        }

        public static bool operator !=(EntityBase<TPrimaryKey, TUserKey> left, EntityBase<TPrimaryKey, TUserKey> right)
        {
            return !(left == right);
        }


    }
}
